package org.sith.gl.taskmanager.client.users;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import org.sith.gl.taskmanager.client.AbstractVerticalPane;
import org.sith.gl.taskmanager.client.UsersRemoteService;
import org.sith.gl.taskmanager.client.beans.UserEntityGWT;

import java.util.List;


public class UsersPane extends AbstractVerticalPane {


    protected void initData() {
        UsersRemoteService.App.getInstance().getAllUsers(new AsyncCallback<List<UserEntityGWT>>() {
            @Override
            public void onFailure(Throwable throwable) {
                Window.alert("Error on server" + throwable.getMessage());
            }

            @Override
            public void onSuccess(List<UserEntityGWT> userEntityGWTs) {
                drawPanel(userEntityGWTs);
            }
        });
    }

    protected void drawPanel(final List<UserEntityGWT> userEntityGWTs) {
        this.clear();
        setWidth("100%");

        
        add(new Label("All users"));
        add(refreshButton);
        final ListBox usersListBox = new ListBox();
        usersListBox.setVisibleItemCount(25);
        usersListBox.setWidth("100%");
        
        usersListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
             usersListBox.getSelectedIndex();
            }
        });
        UsersPane.this.add(usersListBox);
        for (UserEntityGWT userEntityGWT : userEntityGWTs) {
            usersListBox.addItem(userEntityGWT.getLogin());
        }
    }

    @Override
    protected String getPagePosition() {
        return "users";
    }
}
